
var elem_1=new getElem(),main_h=elem_1.main_el.offsetHeight;
window.onload=function(){
	$(".loading").on("touchstart",function(){
		event.preventDefault();
        event.cancelBubble=true;
	}).on("touchmove",function(){
		event.preventDefault();
        event.cancelBubble=true;
	})
	var doc_h=$(document).height();
	window.allPage=$(".page").length;
	window.page_moveTo = new page_move(main_h,allPage);
	$(".page1 .page1_article").css("bottom",$(".page1 .page1_bg").height()+20);
	window.onresize=function(){
		doc_h=$(document).height();
		main_h=elem_1.main_el.offsetHeight;
		page_moveTo.dh=main_h;
		$(".page1 .page1_article").css("bottom",$(".page1 .page1_bg").height()+20);
	}
	
	
	
	elem_1.body_el.addEventListener("touchstart",function(){
		if(page_moveTo.sw){
			page_moveTo.Y=event.touches[0].clientY;
			page_moveTo.YE=event.touches[0].clientY;
			page_moveTo.pageScrollH=page_moveTo.nowPage_el()[0].scrollHeight;
			page_moveTo.pageH=page_moveTo.nowPage_el().height();
			page_moveTo.sectionScrollStart()
		}
	})
	elem_1.body_el.addEventListener("touchmove",function(){
		if(page_moveTo.sw){
			page_moveTo.YE=event.touches[0].clientY;
			page_moveTo.sectionScrollMove()
			page_moveTo.pageScrollT=page_moveTo.nowPage_el().scrollTop();
		}
        event.preventDefault();
        event.cancelBubble=true;
	})
	
	elem_1.body_el.addEventListener("touchend",function(){
		if(page_moveTo.sw){
			page_moveTo.movefn();
			page_moveTo.sectionScrollEnd();
		}
	})
}
function getElem(){
	this.body_el=document.body;
	this.main_el=document.getElementsByClassName("main")[0];
	this.page_el=document.getElementsByClassName("page")[0];
	this.tip_el=document.getElementsByClassName("tip")[0];
	this.wrap_el=document.getElementsByClassName("wrap")[0];
}
//section高度设置
function sections_h(dh,allPage){
	var dh=dh,h;
	for(var i=0;i<allPage;i++){
		h=dh-nowsection(i).offset().top-70+$(".page").eq(i).offset().top;
		nowsection(i).height(h);
	}
}
function nowsection(i){
	return $(".page").eq(i).find($("section"));
}

//页面移动
function page_move(dh,allPage){
	this.dh=dh;
	this.allPage=allPage;
	this.nowPage=0;
	this.Y=0;
	this.YE=0;
	this.pageScrollT=0;
	this.pageScrollH=0;
	this.pageH=0;
	//运动变量
	var thisNowPage=this.nowPage;
	var pageStartScrollT=0;
	var pageScrollMS=0;
	var pageScrollMTime=0;
	var pageScrollMA=0;
	var pageScrollMV=0;
	var pageScrollEndTime=0;
	var pageStartScrollTimer=null;
	var pageEndScrollTimer=null;
	page_move.prototype.swRecord=true;
	page_move.prototype.sw=true;
	page_move.prototype.nowPage_el=function(){
		return $(".page").eq(this.nowPage);
	}
	page_move.prototype.sectionScrollStart=function(){
		clearInterval(pageStartScrollTimer);
		clearInterval(pageEndScrollTimer)
		pageScrollMTime=0;
		pageScrollEndTime=0;
		pageStartScrollT=this.nowPage_el().scrollTop();
		pageStartScrollTimer=setInterval(function(){
			pageScrollMTime++
		},1)
		thisNowPage=this.nowPage;
	}
	
	page_move.prototype.sectionScrollMove=function(){
		pageScrollMS=this.Y-this.YE;
		$(".page").eq(this.nowPage).scrollTop(this.Y-this.YE+pageStartScrollT);

	}
	page_move.prototype.sectionScrollEnd=function(){
		clearInterval(pageStartScrollTimer);
		pageScrollMTime=pageScrollMTime/1000;
		pageScrollMA=2*pageScrollMS/(pageScrollMTime*pageScrollMTime);
		pageScrollMV=2*pageScrollMS/pageScrollMTime;
		pageStartScrollT=this.nowPage_el().scrollTop();
		var pageScrollH=this.pageScrollH,pageH=this.pageH,pageScrollT=this.pageScrollT;
		pageEndScrollTimer=setInterval(function(){
			pageScrollEndTime++
			if(pageScrollMV>0&&(pageScrollMV-pageScrollMA*pageScrollEndTime/1000<=0)||pageScrollMV<0&&(pageScrollMV-pageScrollMA*pageScrollEndTime/1000)>=0||pageStartScrollT<=0||pageScrollH-pageH-pageScrollT<82){
				clearInterval(pageEndScrollTimer)
			}else{
				$(".page").eq(thisNowPage).scrollTop(((pageScrollMV-pageScrollMA*pageScrollEndTime/2000)*pageScrollEndTime/1000)+pageStartScrollT);
			}
		},1)

	}
	
	page_move.prototype.movefn=function(){
		if(this.pageScrollT==0&&this.YE-this.Y>20){
			this.nowPage--;
		}else if(this.pageScrollH-this.pageH-this.pageScrollT<82&&this.YE-this.Y<-20){
			this.nowPage++;
		}else{
			return false;
		}
		
		if(this.nowPage<0){
			this.nowPage=0
		}else if(this.nowPage>allPage-1){
			this.nowPage=allPage-1
		}else{
			if(this.nowPage==this.allPage-1){
				$(".tip").css("display","none")
			}else{
				$(".tip").fadeIn(3000);

			}
			page_move.prototype.sw=false;
			var nowpage=this.nowPage;
			$(".wrap").animate({
				"top":-this.nowPage*this.dh
			},1000,"swing",function(){
				page_animate(nowpage);
				clearTimeout(timer);
				
				if(page_move.prototype.swRecord){
					var timer=setTimeout(function(){
						page_move.prototype.sw=true;
					},1000)
				}else{
					page_move.prototype.sw=true;
				}
				if(nowpage==allPage-1){
					page_move.prototype.swRecord=false;
				}
			})
		}

	}
}


//图片预加载
function imgonload(imgsrc,callback,nowpage){
		var main_img=new Image();
		main_img.src = imgsrc;
		main_img.onload = function(){
			$(".loading").fadeOut()
			callback&&callback(nowpage)
		}
}


//页内动画函数声明
function page_animate(nowpage){}






